package game.frame;

import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class MainFrame {
	
	private int frameX = 250;
	private int frameY = 100;
	private int frameWidth = 800;
	private int frameHeight = 530;
	private static WorkPanelIfc wpIfc = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame  mFrame = new MainFrame();
		JFrame mainFrame = new JFrame("Snake Game");
		JCheckBox grid_cB = new JCheckBox("Show Grid");
		JComboBox mode_CB = new JComboBox();
		WorkPanel wp = new WorkPanel();
		MainFrame.wpIfc = (WorkPanelIfc) wp;
		
		Timer runGame = new Timer();
		
		//-- Setting Frame property
		mFrame.settingFrameProperty(mainFrame);
		mFrame.settingCheckBoxProperty(grid_cB);
		mFrame.settingComboBox(mode_CB);
		
		//-- Add component
		mainFrame.add(wp);
		mainFrame.add(grid_cB);
		mainFrame.add(mode_CB);	
		
		wpIfc.setWorkPanleFocus();
		
		//-- running game
		TimerTask taskRunGame = new TimerTask() {
			
			@Override
			public void run() {
				getWpIfc().updateSnakePos();
			}
		};
		
		runGame.schedule(taskRunGame, 10, 100);
		
		taskRunGame = new TimerTask() {
			
			@Override
			public void run() {
				getWpIfc().getSnakeFood().setChangeColor();
			}
		};
		
		runGame.schedule(taskRunGame, 10, 300);
	}

	private void settingComboBox(JComboBox cb) {
//		cb.setBounds(550, 300, 150, 25);
		cb.setLocation(550, 300);
		cb.setBackground(Color.WHITE);
		cb.setEditable(true);
		cb.setVisible(true);
		
//		cb.addItem(new Integer(1));
//		cb.addItem(new Integer(2));
		
		cb.addItemListener(new ItemListener() {		
			public void itemStateChanged(ItemEvent e) {
				wpIfc.setModeMoving(e.getID() + 1);		
				wpIfc.setWorkPanleFocus();
			}
		});
	}
	
	private void settingCheckBoxProperty(JCheckBox cb) {
		cb.setBounds(550, 100, 150, 25);
		cb.setVisible(true);
		cb.addMouseListener (new MouseAdapter() {

			@Override
			public void mouseReleased(MouseEvent e) {
				super.mouseReleased(e);
					wpIfc.setGridFlag(! wpIfc.isGridFlag());
					wpIfc.setWorkPanleFocus();
			};
		});
	}

	private void settingFrameProperty(JFrame mainFrame) {
		mainFrame.setLayout(null);
		mainFrame.setBounds(frameX, frameY, frameWidth, frameHeight);
		mainFrame.setResizable(false);
		mainFrame.getContentPane().setBackground(Color.BLACK);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
		mainFrame.setVisible(true);
	}

	public static WorkPanelIfc getWpIfc() {
		return wpIfc;
	}

	public static void setWpIfc(WorkPanelIfc wpIfc) {
		MainFrame.wpIfc = wpIfc;
	}

}
